home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11580 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  61 lines

  1. Path: news.rdc.puc-rio.br!usenet
  2. From: Paulo Eduardo Neves <neves@lmf-di.puc-rio.br>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: mixing static and const qualifiers for class members
  5. Date: Thu, 14 Mar 1996 14:31:56 -0600
  6. Organization: Laboratorio de Metodos Formais - PUC-Rio
  7. Message-ID: <3148823C.3F54@lmf-di.puc-rio.br>
  8. References: <4i6mu3$pmg@halon.vggas.com>
  9. NNTP-Posting-Host: wittgenstein.lmf-di.puc-rio.br
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; AIX 1)
  14.  
  15. James Youngman wrote:
  16. > How do I declare and initialise a one-per-class constant?  I want to do
  17. > something like this:
  18. > class Foo
  19. > {
  20. >         static const int nPositions = 6;
  21. >         int nData[nPositions];
  22. > }
  23. > ...how is this done?
  24. > James.
  25.  
  26. To inicialise a ope per class const is easy :
  27.  
  28. in foo.h :
  29.  
  30. class Foo {
  31.   static const int nPositions;
  32. };
  33.  
  34. in foo.C
  35.  
  36. const int Foo::nPositions = 6;
  37.  
  38. Be carefull, if you inicialise it in the header file you will a variable
  39. in each object file. Besides doing this way you can change the value
  40. without having to recompile you code.
  41.  
  42. You also won't be able to declare the array this way, since nPosition
  43. value will just be know at link time. You will have to do something like
  44. that :
  45.  
  46. class Foo {
  47.   Foo():nData(new int[nPositions]){}
  48.   static const int nPositions;
  49.   int *nData;
  50. };
  51.  
  52.  
  53. hope this helps,
  54.  
  55. -- 
  56. Paulo Eduardo Neves             Laboratorio de Metodos Formais - PUC-Rio
  57. mailto:neves@lmf-di.puc-rio.br  Tel.: (021)512-8045
  58.